#include "bookrepository.h"
#include "book.h"

#include <QFileInfo>
#include <QFile>
#include <QTextStream>
#include <QStringList>
#include <QDebug>
#include <QIODevice>

// comment here
BookRepository::BookRepository()
{
	populateRepository();
}

// comment there
void BookRepository::populateRepository()
{
	QFile inputFile( "bookRepo.txt" );
	bool ok;

	if( inputFile.open( QIODevice::ReadOnly ) )
	{
		QTextStream input( &inputFile );

		while( !input.atEnd() )
		{
			const QString dataString = input.readLine();
			const QStringList dataStringList = dataString.split( ", " );

			Book readBookData;
			readBookData.setId( dataStringList.value( 0 ).toInt( &ok ) );
			readBookData.setSharedId( dataStringList.value( 1 ).toInt( &ok ) );
			readBookData.setTitle( dataStringList.value( 2 ) );
			readBookData.setAuthor( dataStringList.value( 3 ) );
			readBookData.setYear( dataStringList.value(4).toInt( &ok ) );
			readBookData.setDescription( dataStringList.value( 5 ) );
			readBookData.setPrice( dataStringList.value( 6 ).toInt( &ok ) );

			if( dataStringList.value( 7 ).toInt( &ok ) == 0 )
			{
				QStringList dateStringList = dataStringList.value( 7 ).split("/");
				QDate date( dateStringList.value(2).toInt(),
				            dateStringList.value(1).toInt(),
				            dateStringList.value(0).toInt() );
				readBookData.setBorrowDate( date );
			}

			if( dataStringList.value( 8 ).toInt( &ok ) == 0 )
			{
				readBookData.setWasBorrowed( false );
			}
			else
			{
				readBookData.setWasBorrowed( true );
			}

			m_bookList.append( readBookData );
		}
	}
	else
	{
		qDebug() << "Failed to open book repository for write";
	}

	inputFile.close();
}

QVector<Book> BookRepository::getList() const
{
	return m_bookList;
}

void BookRepository::updateList(const QVector<Book> &bookList)
{
	m_bookList = bookList;
}

void BookRepository::updateDataBase()
{
	qDebug("updateDataBase()");
	QFile inputFile( "bookRepo.txt" );

	if( inputFile.open( QIODevice::WriteOnly | QIODevice::Truncate ) )
	{
		QTextStream output( &inputFile );

		for( int i = 0; i < m_bookList.size(); i++ )
		{
			ID id = m_bookList.value( i ).getId();
			ID sharedId = m_bookList.value( i ).getSharedId();
			QString title = m_bookList.value( i ).getTitle();
			QString author = m_bookList.value( i ).getAuthor();
			int year = m_bookList.value( i ).getYear();
			QString description = m_bookList.value( i ).getDescription();
			int price = m_bookList.value( i ).getPrice();
			QString isBorrowed;
			int wasBorrowed;
			QDate borrowDate;
			if( ( borrowDate = m_bookList.value( i ).getBorrowDate() ).isValid() )
			{
				isBorrowed = QString::number( borrowDate.day() ) + "/" +
				             QString::number( borrowDate.month() ) + "/" +
				             QString::number( borrowDate.year() );
			}
			else
			{
				isBorrowed = "1";
			}

			if( m_bookList.value( i ).wasBorrowed() == true )
			{
				wasBorrowed = 1;
			}
			else
			{
				wasBorrowed = 0;
			}

			output << id << ", " << sharedId << ", " << title << ", " << author << ", " << year << ", "
			       << description << ", " << price << ", " << isBorrowed << ", " << wasBorrowed << "\n";
		}
	}
	else
	{
		qDebug() << "Failed to open book repository for write";
	}

	if( inputFile.isOpen() )
	{
		inputFile.close();
	}
}
